from fastapi import APIRouter, Depends, Request
from sqlalchemy.orm.session import Session

from server.controller.role import (
    role_add, get_role_list, role_update, role_delete
)
from server.dependencies.check_permission import check_permission
from server.etc.database import create_db
from server.schemas.comment import WriteResponse, SystemErrors
from server.schemas.role import (
    AddRole, UpdateRole, DeleteRole, RolesListResponse
)

router = APIRouter()


@router.post(
    "/addrole",
    response_model=WriteResponse,
    responses={
        500: {"model": SystemErrors, "description": "系统错误"},
    }
)
async def add_role(
        request: Request,
        role:AddRole,
        db:Session = Depends(create_db),
        current_user: str = Depends(check_permission("role:manage"))
):
    """
    添加权限
    """
    result = role_add(request, role, db, current_user)
    return result


@router.get(
    "/getrolelist",
    response_model=RolesListResponse,
    responses={
        500: {"model": SystemErrors, "description": "系统错误"},
    }

)
async def get_role_lists(
        request: Request,
        db:Session = Depends(create_db),
        current_user: str = Depends(check_permission("role:manage"))
):
    """
    获取权限列表
    """
    result = get_role_list(request, db, current_user)
    return result


@router.post(
    "/updaterole",
    response_model=WriteResponse,
    responses={
        500: {"model": SystemErrors, "description": "系统错误"},
    }
)
async def update_role(
        request:Request,
        role:UpdateRole,
        db:Session = Depends(create_db),
        current_user: str = Depends(check_permission("role:manage"))
):
    """
    编辑权限
    """
    result = role_update(request, role, db, current_user)
    return result


@router.post(
    "/deleterole",
    response_model=WriteResponse,
    responses={
        500: {"model": SystemErrors, "description": "系统错误"},
    }
)
async def delete_role(
        request:Request,
        delete_role: DeleteRole,
        db:Session = Depends(create_db),
        current_user: str = Depends(check_permission("role:manage"))
):
    """
    删除权限
    """
    result = role_delete(request, delete_role, db, current_user)
    return result